<?php

class Session
{
    /**
     * @var bool
     */
    static protected $_writeClosed = 0;

    /**
     * @var bool
     */
    static protected $_started = 0;

    /**
     * session use as static class only.
     */
    private function __construct()
    {

    }

    static public function sessionExists()
    {
        if (ini_get('session.use_cookies') == '1' && isset($_COOKIE[session_name()]))
        {
            return true;
        }
        else
        if (!empty($_REQUEST[session_name()]))
        {
            return true;
        }
        return false;
    }

    /**
     * @return bool
     */
    static public function isStarted()
    {
        return self::$_started;
    }

    /**
     * @retrun void
     */
    static public function get()
    {
        return session_name();
    }

    /**
     * set session name
     * @param string $name
     */
    static public function setName($name)
    {
        session_name($name);
    }

    /**
     * get session name
     * @return setring
     */
    static public function getName()
    {
        return session_name();
    }

    /**
     * @return void
     */
    static public function set($name, $value)
    {
        $_SESSION[$name] = $value;
    }
    

    /**
     * check session id
     * @return string
     */
    static public function getId()
    {
        return session_id();
    }

    /**
     * @var string $id
     */
    public static function setId($id)
    {
        session_id($id);
    }

    /**
     * start session
     * @param session start.
     */
    static public function start()
    {
        if (self::$_started)
        {
            return;
        }

        session_start();

        self::$_started = true;

    }

    /**
     * expire session cookie
     * @return void
     */
    static public function expireCookie()
    {
        if (isset($_COOKIE[session_name()]))
        {
            $cookie_params = session_get_cookie_params();

            setcookie(session_name(), false, 315554400, //
            // strtotime('1980-01-01'),
            $cookie_params['path'], $cookie_params['domain'], $cookie_params['secure']);
        }
    }

    /**
     * write session & close
     */
    static public function writeClose()
    {
        if (self::$_writeClosed)
        {
            return;
        }

        session_write_close();

        self::$_writeClosed = true;
    }

    /**
     * destroy session
     * @returnvoid
     */
    static public function destroy()
    {
        session_destroy();
        self::expireCookie();
    }

    /**
     * set session save handler
     */
    static public function setSaveHandler(&$saveHandler)
    {
        session_set_save_handler(array(
            &$saveHandler,
            'open'
        ), array(
            &$saveHandler,
            'close'
        ), array(
            &$saveHandler,
            'read'
        ), array(
            &$saveHandler,
            'write'
        ), array(
            &$saveHandler,
            'destroy'
        ), array(
            &$saveHandler,
            'gc'
        ));
    }

}
